<?php
/**
 * 苏宁控制器
 */

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Libs\SuningLib;
use App\Model\SuningCategoryModel;
use App\Model\SuningGoodsModel;

class SuningController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->middleware('auth');
    }

    /** 高佣类别列表页面 */
    public function category()
    {
        $this->data['title'] = '苏宁类名';
        return view('backend.suning.category', $this->data);
    }

    /** 高佣类别列表操作 */
    public function categoryListPost()
    {

    }

    /** 同步苏宁高佣类别 */
    public function syncCategoryPost()
    {
        $suningSdk = new SuningLib();
        $categoryList = $suningSdk->getInverstmentCategory();
        foreach ($categoryList as $category) {
            $suningModel = SuningCategoryModel::where('sn_category_id', $category['id'])->first();
            $data['name'] = $category['name'];
            $data['sn_category_id'] = $category['id'];
            if ($suningModel) {
                SuningCategoryModel::where('id', $suningModel->id)->update($data);
            } else {
                SuningCategoryModel::insert($data);
            }
        }

        return $this->responseSuccess();
    }

    public function test()
    {
        $a = \App\Model\SidebarModel::listSystemSidebar();
        var_dump($a);
        exit();





        $suningSdk = new SuningLib();
        $pageIndex = 1;
        $data = [];
        do {
            $list = $suningSdk->listInverstmentcommodity($pageIndex, 40);
            foreach ($list as $item) {
                $suningGoodsModel = SuningGoodsModel::where('commodityCode', $item['commodityInfo']['commodityCode'])->select('id')->first();
                $data['firstSaleCategoryId'] = $item['categoryInfo']['firstSaleCategoryId'];
                $data['secondSaleCategoryId'] = $item['categoryInfo']['secondSaleCategoryId'];
                $data['thirdSaleCategoryId'] = $item['categoryInfo']['thirdSaleCategoryId'];
                $data['commodityName'] = $item['commodityInfo']['commodityName'];
                $data['commodityCode'] = $item['commodityInfo']['commodityCode'];
                $data['supplierCode'] = $item['commodityInfo']['supplierCode'];
                $data['supplierName'] = $item['commodityInfo']['supplierName'];
                $data['picUrl'] = isset($item['commodityInfo']['pictureUrl']['0']) ? $item['commodityInfo']['pictureUrl']['0']['picUrl'] : '';
                $data['sellingPoint'] = $item['commodityInfo']['sellingPoint'] ? : '';
                $data['monthSales'] = $item['commodityInfo']['monthSales'] ? : 0;
                $data['commodityPrice'] = $item['commodityInfo']['commodityPrice'] ? : 0;
                $data['snPrice'] = $item['commodityInfo']['snPrice'] ? : 0;
                $data['baoyou'] = $item['commodityInfo']['baoyou'] ? : 0;
                $data['rate'] = $item['commodityInfo']['rate'];
                $data['saleStatus'] = $item['commodityInfo']['saleStatus'] ? : 1;
                $data['productUrl'] = isset($item['commodityInfo']['productUrl']) ? $item['commodityInfo']['productUrl'] : '';
                $data['pgNum'] = (int)$item['pgInfo']['pgNum'];
                $data['pgPrice'] = (float)$item['pgInfo']['pgPrice'];
                $data['pgUrl'] = $item['pgInfo']['pgUrl'];
                if ($suningGoodsModel) {
                    SuningGoodsModel::where('id', $suningGoodsModel->id)->update($data);
                } else {
                    SuningGoodsModel::insert($data);
                }
            }
            $pageIndex++;
        } while (!empty($list));
        return $this->responseSuccess('成功');
    }
}
